package com.guoq.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.guoq.pojo.Result;
import com.guoq.pojo.Sms;
import com.guoq.service.ISmsService;
import com.guoq.utils.IDUtils;
import com.guoq.utils.smsUtils;

/**
 * 
 * <P>类名称:CartController
 * <P>类描述:  购物车Controller
 * <P>创建人:郭强  
 * <P>修改人:XXX 
 * <P>修改时间:2017年7月19日 上午11:45:35
 * <P>修改备注: 
 * @version 1.0.0
 * Copyright(c)2017西部在线-版权所有
 */
@Controller
@RequestMapping("/sms")
public class SmsController {
	@Resource
	private ISmsService smsService;
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: vcode
	 *<P>描述: 注册验证码
	 *@param phonenumber
	 *@return   
	 * @throws Exception 
	 * @exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/vcode")
	@ResponseBody
	public Result vcode(HttpServletRequest request,Model model) throws Exception {
		
		String phonenumber = request.getParameter("phonenumber");
		String vcode = IDUtils.genVcode();
		
		Result result = new Result();
		SendSmsResponse sendSmsResponse = smsUtils.sendSms(phonenumber, "SMS_81265006", vcode);
		smsUtils.querySendDetails(sendSmsResponse.getBizId(), phonenumber);
		
		Sms sms = smsUtils.getSmsInfo(sendSmsResponse.getBizId(), phonenumber, vcode);
		result = this.smsService.create(sms);
		//请求成功
		if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
			result.setStatus(200);
			result.setMsg("验证码请求成功");
		}else{
			result.setStatus(500);
			result.setMsg("验证码请求失败，请稍后再试");
		}
		
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: retrievepassword
	 *<P>描述: 找回密码
	 *@param phonenumber
	 *@return   
	 * @throws ClientException 
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/retrievepassword")
	@ResponseBody
	public Result retrievepassword(String phonenumber) throws ClientException {
		
		String vcode = IDUtils.genVcode();
		Result result = new Result();
		SendSmsResponse sendSmsResponse = smsUtils.sendSms(phonenumber, "SMS_81530009", vcode);
		smsUtils.querySendDetails(sendSmsResponse.getBizId(), phonenumber);
		
		//请求成功
		if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
			result.setStatus(200);
			result.setMsg("验证码请求成功");
		}else{
			result.setStatus(500);
			result.setMsg("验证码请求失败，请稍后再试");
		}
		return result;
	}
	
	
}
